package com.itheima.prize.api.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.prize.commons.db.entity.CardUser;
import com.itheima.prize.commons.db.entity.CardUserDto;
import com.itheima.prize.commons.db.entity.ViewCardUserHit;
import com.itheima.prize.commons.db.service.GameLoadService;
import com.itheima.prize.commons.db.service.ViewCardUserHitService;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PageBean;
import com.itheima.prize.commons.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping(value = "/api/user")
@Api(tags = {"用户模块"})
public class UserController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ViewCardUserHitService hitService;
    @Autowired
    private GameLoadService loadService;

    @GetMapping("/info")
    @ApiOperation(value = "用户信息")
    public ApiResult info(HttpServletRequest request) {
        //TODO：任务3.3-用户模块-用户信息
        CardUser user = (CardUser) request.getSession().getAttribute("user");
        if(user==null){
            return new ApiResult<>(0, "登录超时", null);
        }
        //构造函数拷贝属性
        CardUserDto userDto = new CardUserDto(user);
        Integer id = userDto.getId();
        //根据id查询参与活动数与中奖数统计
        userDto.setGames(loadService.getGamesNumByUserId(id));
        userDto.setProducts(loadService.getPrizesNumByUserId(id));
        return new ApiResult<>(1, "成功", userDto);
    }

    @GetMapping("/hit/{gameid}/{curpage}/{limit}")
    @ApiOperation(value = "我的奖品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gameid", value = "活动id（-1=全部）", dataType = "int", example = "1", required = true),
            @ApiImplicitParam(name = "curpage", value = "第几页", defaultValue = "1", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", dataType = "int", example = "3")
    })
    public ApiResult hit(@PathVariable int gameid, @PathVariable int curpage, @PathVariable int limit, HttpServletRequest request) {
        //TODO：任务3.4-用户模块-我的奖品
        CardUser user = (CardUser) request.getSession().getAttribute("user");
        Integer userId = user.getId();
        // 创建分页查询条件
        QueryWrapper<ViewCardUserHit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userId); // 筛选当前用户的奖品记录
        // 如果 gameid 不为 -1，则按活动 ID 筛选
        if (gameid != -1) {
            queryWrapper.eq("gameid", gameid);
        }

        // 执行分页查询
        Page<ViewCardUserHit> page = new Page<>(curpage, limit);
        Page<ViewCardUserHit> resultPage = hitService.page(page, queryWrapper);

        // 将分页结果封装到 PageBean 中设置
        PageBean<ViewCardUserHit> pageBean = new PageBean<>(resultPage);

        // 封装到 ApiResult 中并返回
        return new ApiResult<>(1, "查询成功", pageBean);
    }


}